﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Microsoft.Win32;
using System.Windows;
using System.Xml;
using WinForm = System.Windows.Forms;
using System.IO;

namespace SHomeWorkshop.LunarMind.Commands
{
    /// <summary>
    /// 以此类为模板，制作自定义命令类
    ///     共需要修改１０处！！！
    /// ==============================
    /// 
    /// 缩小标题文本命令。
    /// ★①，修改类名。
    /// </summary>
    public static class ReduceTextCommand
    {
        private static RoutedUICommand routedUICmd;//创建私有字段

        public static RoutedUICommand RoutedUICmd//创建只读静态属性
        {
            get { return routedUICmd; }
        }

        /// <summary>
        /// ★②，修改静态构造方法名。
        /// </summary>
        static ReduceTextCommand()//类型构造器
        {
            //★③，修改两个字符串参数名。★④以及第三个参数的类型名。
            routedUICmd = new RoutedUICommand("ReduceTextCommand", "ReduceTextCommand",
                typeof(ReduceTextCommand),//创建RoutedUICommand对象
                new InputGestureCollection() 
                { 
                    //★⑤，修改此处两个参数，以便挂接快捷键。
                    new KeyGesture(Key.OemComma,ModifierKeys.Control| ModifierKeys.Shift,"Ctrl+Shift+<")
                });

            cmdBinding.Command = routedUICmd;
            cmdBinding.CanExecute += new CanExecuteRoutedEventHandler(cmdBinding_CanExecute);
            cmdBinding.Executed += new ExecutedRoutedEventHandler(cmdBinding_Executed);
        }

        private static CommandBinding cmdBinding = new CommandBinding();

        public static CommandBinding CmdBinding
        {
            get { return cmdBinding; }
        }

        static void cmdBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            //★⑦，修改此方法的实现。

            ReduceText();
        }

        static void ReduceText()
        {
            var selectedTitles = Globals.MainWindow.SelectedTitlesList;
            if (selectedTitles == null || selectedTitles.Count <= 0)
            {
                //看有没有Comment被选中。
                var mainSelComment = Globals.MainWindow.SelectedComment;//comment不允许多选。而Title可以。
                if (mainSelComment != null)
                {
                    ModifingItem mi = new ModifingItem()
                    {
                        IsNeedRefreshTitleAndLinesLocation = true,
                        ModifingName = "缩小备注块文本"
                    };

                    XmlNode textSetNode = mainSelComment.XmlData.SelectSingleNode("TextSet");
                    if (textSetNode != null)
                    {
                        var textNodes = textSetNode.ChildNodes;
                        if (textNodes != null && textNodes.Count > 0)
                        {
                            int changedCount = 0;
                            foreach (XmlNode textNode in textNodes)
                            {
                                var fontSizeAttr = textNode.GetAttribute("FontSize");
                                if (fontSizeAttr != null)
                                {
                                    try
                                    {
                                        double fontsizeValue = double.Parse(fontSizeAttr.Value);
                                        double oldVal = fontsizeValue;
                                        fontsizeValue -= 2;

                                        if (fontsizeValue < 6) fontsizeValue = 6;//最小字号6

                                        if (oldVal != fontsizeValue)
                                        {
                                            changedCount++;
                                            fontSizeAttr.Value = fontsizeValue.ToString();
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        MessageBox.Show(ex.Message);
                                    }
                                }
                                else
                                {
                                    double fontsizeValue = 15;//默认字号为15.
                                    double oldVal = fontsizeValue;
                                    fontsizeValue -= 2;

                                    if (fontsizeValue < 6) fontsizeValue = 6;//最小字号6

                                    if (oldVal != fontsizeValue)
                                    {
                                        changedCount++;
                                        textNode.SetAttribute("FontSize", fontsizeValue.ToString());
                                    }
                                }
                            }

                            if (changedCount > 0)
                            {
                                //textSetNode.InnerXml = actXmlDataInnerXml;
                                Action actXmlDataInnerXml = new Action(mainSelComment.ID, -1, mainSelComment.GetType().Name,
                                    "XmlDataInnerXml", textSetNode.InnerXml, textSetNode.InnerXml);
                                mi.Add(actXmlDataInnerXml);

                                mainSelComment.BuildComment();
                            }

                            if (mi.ModifiedPropertyAndValues.Count > 0)
                            {
                                Globals.MainWindow.Do(mi);
                                Globals.MainWindow.IsModified = true;
                            }
                        }
                    }
                }

                return;
            }
            else
            {
                //以下为缩小标题文本。较复杂。
                ModifingItem mi = new ModifingItem()
                {
                    IsNeedRefreshTitleAndLinesLocation = true,
                    ModifingName = "缩小标题文本"
                };

                foreach (Title t in selectedTitles)
                {
                    XmlNode textSetNode = t.XmlData.SelectSingleNode("TextSet");
                    if (textSetNode != null)
                    {
                        var textNodes = textSetNode.ChildNodes;
                        if (textNodes != null && textNodes.Count > 0)
                        {
                            int changedCount = 0;
                            foreach (XmlNode textNode in textNodes)
                            {
                                var fontSizeAttr = textNode.GetAttribute("FontSize");
                                if (fontSizeAttr != null)
                                {
                                    try
                                    {
                                        double fontsizeValue = double.Parse(fontSizeAttr.Value);
                                        double oldVal = fontsizeValue;
                                        fontsizeValue -= 2;

                                        if (fontsizeValue < 6) fontsizeValue = 6;//最小字号6

                                        if (oldVal != fontsizeValue)
                                        {
                                            changedCount++;
                                            fontSizeAttr.Value = fontsizeValue.ToString();
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        MessageBox.Show(ex.Message);
                                    }
                                }
                            }

                            if (changedCount > 0)
                            {
                                //textSetNode.InnerXml = actXmlDataInnerXml;
                                Action actXmlDataInnerXml = new Action(t.ID, -1, t.GetType().Name,
                                    "XmlDataInnerXml", textSetNode.InnerXml, textSetNode.InnerXml);
                                mi.Add(actXmlDataInnerXml);
                            }
                        }
                    }

                    XmlNode commentSetNode = t.XmlData.SelectSingleNode("CommentSet");
                    if (commentSetNode != null)
                    {
                        var commentNodes = commentSetNode.ChildNodes;
                        if (commentNodes != null && commentNodes.Count > 0)
                        {
                            int changedCount = 0;
                            foreach (XmlNode commentNode in commentNodes)
                            {
                                var fontSizeAttr = commentNode.GetAttribute("FontSize");
                                if (fontSizeAttr != null)
                                {
                                    try
                                    {
                                        double fontsizeValue = double.Parse(fontSizeAttr.Value);
                                        double oldVal = fontsizeValue;

                                        fontsizeValue -= 2;

                                        if (fontsizeValue < 6) fontsizeValue = 6;//最小字号6

                                        if (oldVal != fontsizeValue)
                                        {
                                            changedCount++;
                                            fontSizeAttr.Value = fontsizeValue.ToString();
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        MessageBox.Show(ex.Message);
                                    }
                                }
                            }

                            if (changedCount > 0)
                            {
                                //textSetNode.InnerXml = actXmlDataInnerXml;
                                Action actCommentXmlDataInnerXml = new Action(t.ID, -1, t.GetType().Name,
                                    "XmlCommentInnerXml", commentSetNode.InnerXml, commentSetNode.InnerXml);
                                mi.Add(actCommentXmlDataInnerXml);
                            }
                        }
                    }

                    if (mi.ModifiedPropertyAndValues.Count > 0)
                    {
                        mi.IsNeedRefreshTitleAndLinesLocation = true;
                        t.BuildTitle();
                    }
                }

                if (mi.ModifiedPropertyAndValues.Count > 0)
                {
                    Globals.MainWindow.RefreshTitleAndLinesLocation();
                    Globals.MainWindow.Do(mi);
                    Globals.MainWindow.IsModified = true;
                }
            }
        }

        static void cmdBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            //★⑧，修改此方法的实现。
            var list = Globals.MainWindow.SelectedTitlesList;
            if (list != null && list.Count > 0)
            {
                e.CanExecute = true; return;
            }

            if (Globals.MainWindow.SelectedComment != null)
            {
                e.CanExecute = true; return;
            }


            e.CanExecute = false;
        }
    }
}
